import requests

from lxml import etree
# 发送的地址
url = 'http://nba.hupu.com/stats/players'
headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107.0.1418.26'}

# 发送请求
resp = requests.get(url,headers = headers)
print(resp.text)
# 处理结果
e = etree.HTML(resp.text)
# 解析相应数据
nos = e.xpath('//table[@class="players_table"]//tr/td[1]/text()')
names = e.xpath('//table[@class="players_table"]//tr/td[2]/a/text()')
teams = e.xpath('//table[@class="players_table"]//tr/td[3]/a/text()')
scores = e.xpath('//table[@class="players_table"]//tr/td[4]/text()')

nos[0] = ' '
names.insert(0,'         ')
teams.insert(0,'     ')
scores[0] = ' '

# 是否保存
with open('nba.txt','w',encoding='utf-8') as f:
    for no,name,team,score in zip(nos,names,teams,scores):
        f.write(f'排名：{no} 姓名：{name} 球队：{team} 得分：{score}\n')